<template>
    <div class="app-container">
        <div class="Search">
            <!--工具条-->
            <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
                <el-form :inline="true" :model="query">
                  
                <el-form-item>
                    <el-cascader ref="organTree" placeholder="所属组织" :props="props" :options="optionData" @change="selectOrgan" clearable collapse-tags filterable></el-cascader>
                </el-form-item>
                <el-form-item>
                    <el-input v-model="query.params.templateId" placeholder="模板编号" />
                </el-form-item>
                <el-form-item>
                    <el-input v-model="query.params.aisleName" placeholder="货道名称" />
                </el-form-item>

                <el-form-item>
                    <el-input v-model="query.params.goodsId" placeholder="商品编号" />
                </el-form-item>
                <el-form-item>
                    <el-input v-model="query.params.goodsName" placeholder="商品名称" />
                </el-form-item>
                <el-form-item>
                    <el-input v-model="query.params.goodsSpec" placeholder="商品规格" />
                </el-form-item>
                <el-form-item>
                    <el-input v-model="query.params.goodsProductPlace" placeholder="商品产地" />
                </el-form-item>
                 <el-form-item>
                    <el-select v-model="query.params.goodsPrice" placeholder="商品单价">
                        <el-option
                                v-for="item in query.options"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-select v-model="query.params.goodsCost" placeholder="商品成本">
                        <el-option
                                v-for="item in query.options"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value">
                        </el-option>
                    </el-select>
                </el-form-item>
                                                                      
                                               

                <el-form-item>
                    <el-select v-model="query.params.aisleType" placeholder="货道类型">
                        <el-option
                                v-for="item in query.options"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value">
                        </el-option>
                    </el-select>
                </el-form-item>
                                             
                <el-form-item>
                    <el-select v-model="query.params.ctrlBoard" placeholder="控制板号">
                        <el-option
                                v-for="item in query.options"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-select v-model="query.params.ctrlCmd" placeholder="控制指令">
                        <el-option
                                v-for="item in query.options"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-select v-model="query.params.status" placeholder="货道状态">
                        <el-option
                                v-for="item in query.options"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value">
                        </el-option>
                    </el-select>
                </el-form-item>
              
                <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="fetchData">查询</el-button>
                </el-form-item>
                <el-form-item>
                <el-button type="success" icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
                </el-form-item>
            </el-form>
        </el-col>
    </div>

        <div class="block">
            <el-table
                    v-loading="listLoading"
                    :data="list"
                    element-loading-text="Loading"
                    border
                    fit
                    highlight-current-row
            >
                <el-table-column align="center" label="序号" width="95">
                    <template slot-scope="scope">
                        {{ scope.$index }}
                    </template>
                </el-table-column>

                <el-table-column label="主键" width="110">
                <template slot-scope="scope">
                {{ scope.row.id }}
                </template>
                </el-table-column>
                <el-table-column label="模板编号" width="110">
                <template slot-scope="scope">
                {{ scope.row.templateId }}
                </template>
                </el-table-column>
                <el-table-column label="所属机构" width="110">
                <template slot-scope="scope">
                {{ scope.row.organId }}
                </template>
                </el-table-column>
                <el-table-column label="货道名称" width="110">
                <template slot-scope="scope">
                {{ scope.row.aisleName }}
                </template>
                </el-table-column>
                <el-table-column label="货道数量" width="110">
                <template slot-scope="scope">
                {{ scope.row.printCount }}
                </template>
                </el-table-column>
                <el-table-column label="货道类型" width="110">
                <template slot-scope="scope">
                {{ scope.row.aisleType }}
                </template>
                </el-table-column>
                <el-table-column label="商品编号" width="110">
                <template slot-scope="scope">
                {{ scope.row.goodsId }}
                </template>
                </el-table-column>
                <el-table-column label="商品名称" width="110">
                <template slot-scope="scope">
                {{ scope.row.goodsName }}
                </template>
                </el-table-column>
                <el-table-column label="商品规格" width="110">
                <template slot-scope="scope">
                {{ scope.row.goodsSpec }}
                </template>
                </el-table-column>
                <el-table-column label="商品产地" width="110">
                <template slot-scope="scope">
                {{ scope.row.goodsProductPlace }}
                </template>
                </el-table-column>
                <el-table-column label="商品单价" width="110">
                <template slot-scope="scope">
                {{ scope.row.goodsPrice }}
                </template>
                </el-table-column>
                <el-table-column label="商品成本" width="110">
                <template slot-scope="scope">
                {{ scope.row.goodsCost }}
                </template>
                </el-table-column>
                <el-table-column label="商品优惠价" width="110">
                <template slot-scope="scope">
                {{ scope.row.goodsDiscountPrice }}
                </template>
                </el-table-column>
                <el-table-column label="商品图片" width="110">
                     <template slot-scope="scope">
                        <el-image
                          style="width: 100px; height: 100px"
                          :src="scope.row.goodsImg" >
                        </el-image>
                    </template>
                </el-table-column>
                <el-table-column label="最低价" width="110">
                <template slot-scope="scope">
                {{ scope.row.basePrice }}
                </template>
                </el-table-column>
                
                <el-table-column label="是否已删除" width="110">
                <template slot-scope="scope">
                {{ scope.row.deleted }}
                </template>
                </el-table-column>
                <el-table-column label="是否打印完成" width="110">
                <template slot-scope="scope">
                {{ scope.row.isDone }}
                </template>
                </el-table-column>
                <el-table-column label="是否热销,0:否，1:是" width="110">
                <template slot-scope="scope">
                {{ scope.row.isHot }}
                </template>
                </el-table-column>
                <el-table-column label="控制板号" width="110">
                <template slot-scope="scope">
                {{ scope.row.ctrlBoard }}
                </template>
                </el-table-column>
                <el-table-column label="控制指令" width="110">
                <template slot-scope="scope">
                {{ scope.row.ctrlCmd }}
                </template>
                </el-table-column>
                <el-table-column label="货道状态" width="110">
                <template slot-scope="scope">
                {{ scope.row.status }}
                </template>
                </el-table-column>
                <el-table-column label="所属组织路径" width="110">
                <template slot-scope="scope">
                {{ scope.row.organPath }}
                </template>
                </el-table-column>
                <el-table-column  fixed="right"  align="center" prop="created_at" label="操作" width="200">
                    <template slot-scope="scope">
                        <span class="el-tag el-tag--info el-tag--mini" style="cursor: pointer;" @click="handleEdit(scope.$index, scope.row)">
                          {{ scope.row.isSet?'保存':"修改" }}
                        </span>
                        <span v-if="!scope.row.isSet" class="el-tag el-tag--danger el-tag--mini" style="cursor: pointer;" @click="handleDelete(scope.$index, scope.row)">
                          删除
                        </span>
                    </template>
                </el-table-column>
            </el-table>
        </div>

        <div class="block">
            <el-pagination
                    :current-page="currentPage"
                    :page-sizes="[10, 20, 30, 40]"
                    :page-size="pageSize"
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="total"
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
            />
        </div>

        <div class="block">
            <!--弹框-->
            <el-dialog :title="titleMap[dialogStatus]" :visible.sync="dialogFormVisible">
                <el-form :model="form">

                    <el-form-item v-if="addForItemVisible" label="主键">
                        <el-input v-model="form.id" placeholder="主键"  auto-complete="off"/>
                    </el-form-item>
                    <el-form-item label="模板编号">
                        <el-input v-model="form.templateId" placeholder="模板编号"  auto-complete="off"/>
                    </el-form-item>
                    <el-form-item label="所属机构">
                        <el-input v-model="form.organId" placeholder="所属机构"  auto-complete="off"/>
                    </el-form-item>
                    <el-form-item label="商品编号">
                        <el-input v-model="form.goodsId" placeholder="商品编号"  auto-complete="off"/>
                    </el-form-item>
                    <el-form-item label="商品名称">
                        <el-input v-model="form.goodsName" placeholder="商品名称"  auto-complete="off"/>
                    </el-form-item>
                    <el-form-item label="商品规格">
                        <el-input v-model="form.goodsSpec" placeholder="商品规格"  auto-complete="off"/>
                    </el-form-item>
                    <el-form-item label="商品产地">
                        <el-input v-model="form.goodsProductPlace" placeholder="商品产地"  auto-complete="off"/>
                    </el-form-item>
                    <el-form-item label="商品单价">
                        <el-select v-model="form.goodsPrice" placeholder="商品单价">
                            <el-option
                                    v-for="item in query.options"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value">
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="商品成本">
                        <el-select v-model="form.goodsCost" placeholder="商品成本">
                            <el-option
                                    v-for="item in query.options"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value">
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="商品优惠价">
                        <el-select v-model="form.goodsDiscountPrice" placeholder="商品优惠价">
                            <el-option
                                    v-for="item in query.options"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value">
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="商品图片">
                        <el-input v-model="form.goodsImg" placeholder="商品图片"  auto-complete="off"/>
                    </el-form-item>
                    <el-form-item label="货道名称">
                        <el-input v-model="form.aisleName" placeholder="货道名称"  auto-complete="off"/>
                    </el-form-item>
                    <el-form-item label="打印数量">
                        <el-select v-model="form.printCount" placeholder="打印数量">
                            <el-option
                                    v-for="item in query.options"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value">
                            </el-option>
                        </el-select>
                    </el-form-item>
                                        <el-form-item label="货道类型，0：弹簧，1：履带, 2：格子锁">
                        <el-select v-model="form.aisleType" placeholder="货道类型，0：弹簧，1：履带, 2：格子锁">
                            <el-option
                                    v-for="item in query.options"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value">
                            </el-option>
                        </el-select>
                    </el-form-item>
                                <el-form-item label="是否已删除">
                        <el-input v-model="form.deleted" placeholder="是否已删除"  auto-complete="off"/>
                    </el-form-item>
                                                <el-form-item label="是否打印完成">
                        <el-select v-model="form.isDone" placeholder="是否打印完成">
                            <el-option
                                    v-for="item in query.options"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value">
                            </el-option>
                        </el-select>
                    </el-form-item>
                                    <el-form-item label="是否热销,0:否，1:是">
                    <el-select v-model="form.isHot" placeholder="是否热销,0:否，1:是">
                        <el-option
                                v-for="item in query.options"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value">
                        </el-option>
                    </el-select>
                </el-form-item>
                                    <el-form-item label="控制板号">
                    <el-select v-model="form.ctrlBoard" placeholder="控制板号">
                        <el-option
                                v-for="item in query.options"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value">
                        </el-option>
                    </el-select>
                </el-form-item>
                                    <el-form-item label="控制指令">
                    <el-select v-model="form.ctrlCmd" placeholder="控制指令">
                        <el-option
                                v-for="item in query.options"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value">
                        </el-option>
                    </el-select>
                </el-form-item>
                                    <el-form-item label="货道状态；1:正常, 2:停用,3:异常">
                    <el-select v-model="form.status" placeholder="货道状态；1:正常, 2:停用,3:异常">
                        <el-option
                                v-for="item in query.options"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value">
                        </el-option>
                    </el-select>
                </el-form-item>
                                    <el-form-item label="排序">
                    <el-select v-model="form.sort" placeholder="排序">
                        <el-option
                                v-for="item in query.options"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value">
                        </el-option>
                    </el-select>
                </el-form-item>
                            <el-form-item label="">
                    <el-input v-model="form.createBy" placeholder=""  auto-complete="off"/>
                </el-form-item>
                                    <el-form-item label="">
                    <el-input v-model="form.updateBy" placeholder=""  auto-complete="off"/>
                </el-form-item>
                                        <el-form-item label="">
                    <el-date-picker v-model="form.createDate" type="date" placeholder="">
                    </el-date-picker>
                </el-form-item>
                                    <el-form-item label="">
                    <el-date-picker v-model="form.updateDate" type="date" placeholder="">
                    </el-date-picker>
                </el-form-item>
                                <el-form-item label="">
                    <el-input v-model="form.remark" placeholder=""  auto-complete="off"/>
                </el-form-item>
                                            <el-form-item label="最低价">
                    <el-select v-model="form.basePrice" placeholder="最低价">
                        <el-option
                                v-for="item in query.options"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value">
                        </el-option>
                    </el-select>
                </el-form-item>
                                            <el-form-item label="所属组织路径">
                    <el-input v-model="form.organPath" placeholder="所属组织路径"  auto-complete="off"/>
                </el-form-item>
                </el-form>

                <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取 消</el-button>
                <el-button type="primary" @click="handleEditSave">确 定</el-button>
                </div>
            </el-dialog>
        </div>
    </div>
    </div>

</template>

<script>
    import { getList, doDelete, doEdit } from '@/api/deviceAisleTemplateItem/'
    import { getDictionaryGroup , getReferencedDictionaryList  } from '@/api/dictionary'
    import { getList as getOrganList, querySubordinateOrgan } from '@/api/Organ'
    import { getOrganId, getTenantId, getOrganPath, getCurrentLoginUserId, handleBuildTreeDataStruct , getSelectedNodes , getSelectedNodesObject } from '@/api/utils'  
    import localStorageUtil from '@/utils/localStorageUtil'


    export default {
        data() {
            return {
                list: null,
                listLoading: true,
                total: 5,
                currentPage: 1,
                pageSize: 10,
                dialogFormVisible: false,
                addForVisible: false,
                form: {
                    id:'',
                    templateId:'',
                    organId:'',
                    goodsId:'',
                    goodsName:'',
                    goodsSpec:'',
                    goodsProductPlace:'',
                    goodsPrice:'',
                    goodsCost:'',
                    goodsDiscountPrice:'',
                    goodsImg:'',
                    aisleName:'',
                    printCount:'',
                    aisleType:'',
                    deleted:'',
                    isDone:'',
                    isHot:'',
                    ctrlBoard:'',
                    ctrlCmd:'',
                    status:'',
                    sort:'',
                    createBy:'',
                    updateBy:'',
                    createDate:'',
                    updateDate:'',
                    remark:'',
                    basePrice:'',
                    isDeleted:'',
                    organPath:''
                },
                // 新增or编辑弹框标题(根据点击的新增or编辑按钮显示不同的标题)
                titleMap: {
                    add: '新增',
                    edit: '编辑'
                },
                props: { // 配置项（必选）
                    value: 'id',
                    label: 'organName',
                    children: 'children',
                    title: '组织',
                    multiple: false,
                    checkStrictly: true,
                    emitPath:false
                    // disabled:true
                },
                optionData: [{ id: '1', parentId: '2', organName: '平台', children: [] }],

                // 新增和编辑弹框标题
                dialogStatus: '',
                query: {
                    params: {
                        id:'',
                        templateId:'',
                        organId:'',
                        goodsId:'',
                        goodsName:'',
                        goodsSpec:'',
                        goodsProductPlace:'',
                        goodsPrice:'',
                        goodsCost:'',
                        goodsDiscountPrice:'',
                        goodsImg:'',
                        aisleName:'',
                        printCount:'',
                        aisleType:'',
                        deleted:'',
                        isDone:'',
                        isHot:'',
                        ctrlBoard:'',
                        ctrlCmd:'',
                        status:'',
                        sort:'',
                        createBy:'',
                        updateBy:'',
                        createDate:'',
                        updateDate:'',
                        remark:'',
                        basePrice:'',
                        isDeleted:'',
                        organPath:''
                    },
                    timeValue: '',
                    options: [{
                        value: '1',
                        label: '黄金糕'
                    }, {
                        value: '2',
                        label: '双皮奶'
                    }],
                    value: ''
                }
            }
        },
        created() {
             this.initOrganTree(()=>{
                this.setOrganQueryType()
            })
            this.fetchData()
        },
        methods: {
            fetchData() {
                this.listLoading = true
                this.query.params.organPath = this.query.params.organPath||getOrganPath()
                getList(this.query.params , this.currentPage, this.pageSize).then(response => {
                    const data = response.data
                    this.total = data.totalCount
                this.pageSize = data.pageSize
                this.currentPage = data.currentPage
                this.list = data.entities
                this.listLoading = false
            })
            },
            handleSizeChange(val) {
                this.currentPage = 1
                this.pageSize = val
            },
            handleCurrentChange(val) {
                this.currentPage = val
            },
            // 新增
            handleAdd() {
                // 显示弹框
                this.dialogFormVisible = true
                // 新增弹框标题
                this.dialogStatus = 'add'

                this.form.id=''
                this.form.templateId=''
                this.form.organId=''
                this.form.goodsId=''
                this.form.goodsName=''
                this.form.goodsSpec=''
                this.form.goodsProductPlace=''
                this.form.goodsPrice=''
                this.form.goodsCost=''
                this.form.goodsDiscountPrice=''
                this.form.goodsImg=''
                this.form.aisleName=''
                this.form.printCount=''
                this.form.aisleType=''
                this.form.deleted=''
                this.form.isDone=''
                this.form.isHot=''
                this.form.ctrlBoard=''
                this.form.ctrlCmd=''
                this.form.status=''
                this.form.sort=''
                this.form.createBy=''
                this.form.updateBy=''
                this.form.createDate=''
                this.form.updateDate=''
                this.form.remark=''
                this.form.basePrice=''
                this.form.isDeleted=''
                this.form.organPath=''
                this.addForVisible = false
            },
            // 编辑
            handleEdit(index, row) {
                console.log(index)
                console.log(row)
                this.form.id=row.id
                this.form.templateId=row.templateId
                this.form.organId=row.organId
                this.form.goodsId=row.goodsId
                this.form.goodsName=row.goodsName
                this.form.goodsSpec=row.goodsSpec
                this.form.goodsProductPlace=row.goodsProductPlace
                this.form.goodsPrice=row.goodsPrice
                this.form.goodsCost=row.goodsCost
                this.form.goodsDiscountPrice=row.goodsDiscountPrice
                this.form.goodsImg=row.goodsImg
                this.form.aisleName=row.aisleName
                this.form.printCount=row.printCount
                this.form.aisleType=row.aisleType
                this.form.deleted=row.deleted
                this.form.isDone=row.isDone
                this.form.isHot=row.isHot
                this.form.ctrlBoard=row.ctrlBoard
                this.form.ctrlCmd=row.ctrlCmd
                this.form.status=row.status
                this.form.sort=row.sort
                this.form.createBy=row.createBy
                this.form.updateBy=row.updateBy
                this.form.createDate=row.createDate
                this.form.updateDate=row.updateDate
                this.form.remark=row.remark
                this.form.basePrice=row.basePrice
                this.form.isDeleted=row.isDeleted
                this.form.organPath=row.organPath
                this.addForVisible = true
                // 显示弹框
                this.dialogFormVisible = true
                // 编辑弹框标题
                this.dialogStatus = 'edit'
            },
            handleDelete(index, row) {
                console.log(index)
                console.log(row)

                this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning',
                    center: true
                }).then(() => {
                    doDelete(row.id).then(response => {
                    const data = response.data
                    console.log(data)
                    if(data.status == 1){
                        this.$message({
                            type: 'success',
                            message: '操作成功!'
                        })
                        this.fetchData();
                    }else if(data.status == 403){
                        this.$message({
                            type: 'warning',
                            message: data.description
                        })
                }else{
                    this.$message({
                        type: 'info',
                        message: '操作失败!'
                    })
                }
            })
            }).catch(() => {
                    this.$message({
                    type: 'info',
                    message: '已取消删除'
                })
            })
            },
            handleEditSave() {
                debugger

                doEdit(this.form).then(response => {
                    debugger
                            console.log(response)
                    const data = response.data
                    if(data.status){
                        this.$message({
                            type: 'success',
                            message: '操作成功!'
                        })
                     this.fetchData();
                    }else{
                        this.$message({
                            type: 'info',
                            message: '操作失败!'
                        })
                    }
                    this.listLoading = false
                    this.dialogFormVisible = false
            })
            },
            handleReset() {
                this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning',
                    center: true
                }).then(() => {
                    this.$message({
                    type: 'success',
                    message: '删除成功!'
                })
            }).catch(() => {
                    this.$message({
                    type: 'info',
                    message: '已取消删除'
                })
            })
            },
            initOrganTree(fn) {
              this.listLoading = true
              querySubordinateOrgan({ organId: getOrganId() }).then(response => {
                  console.log(response)
                  const data = response.data.data
                  this.optionData = handleBuildTreeDataStruct(data, 'id', 'parentId')
                  this.listLoading = false
                  fn()
              })
            },
            setOrganQueryType(){
              this.organQueryModelChange(localStorageUtil.fetch("SYSTEM_SETTING_ORGAN_QUERY_MODEL"))
            },
            selectOrgan(nodes){
                let nodesInfo = this.$refs['organTree'].getCheckedNodes()
                let that = this
                this.query.params.organPath = ''
                this.query.params.organIds = []
                if (this.props.multiple) {
                    this.query.params.organIds = []
                    if (nodesInfo.length) {
                        nodesInfo.forEach(function(element){
                            that.query.params.organIds.push(element.data.id)
                        })
                    }
                } else {             
                    this.query.params.organPath = nodesInfo[0].data.organPath
                }
            },
            organQueryModelChange(organQueryModel) {
              if(organQueryModel == 'selected') {
                  this.props.multiple=true
              }else if(organQueryModel == 'selfAndSub') {
                  this.props.multiple=false
              }
            }

        }
    }
</script>
